import json
import os

import requests

from Base import Log, GetYamlFile
from Base.HttpBase import HttpBase
from Base.ReadExcel import Read_Excel

log = Log.Log()
# base_url = 'http://192.168.17.236:30810'  # 独立租户2环境
# base_url = 'http://192.168.17.236:30510'  # 独立租户1环境
# tenant_id = 1  # 独立租户1环境
# base_url = 'http://111.20.218.54:30510'  # 凤县线上环境
base_url = 'http://61.185.114.170:30510'  # 铜川线上环境
# 需要手动修改所需环境id 及name
tenant_id = 2185
tenant_name = '铜川声威建材有限责任公司'

login_api = base_url + '/upms-provider/tenant/user/login/mobile'
login_data = {
    'mobile': '18368093710',
    'password': 'Aa123456'
}
headers = {
    'Content-Type': 'application/json'
}

is_list = []

token = HttpBase(login_api, login_data, 'post', headers).getHttpHeaders().get('access_token')
Refresh_token = HttpBase(login_api, login_data, 'post', headers).getHttpHeaders().get('Refresh_token')
log.info(token)
# 基础数据

# 基础请求头
base_headers = {
    'type': 'tenant',
    'tenant_id': str(tenant_id),
    'access_token': token,
    'Refresh_token': Refresh_token,
    # 'Version': 'middleground-2.0.5.6-SNAPSHOT'
}


def car_add():
    yml_list = GetYamlFile.getYamlFile()
    carPath = yml_list["carPath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, carPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    print(num)
    list1 = []
    for i in range(1, num):
        license_plate = readBook.read_Excel("sheet", i, 0)
        car_add_url = base_url + '/tenant-lms-basic-provide/lms/basic/car/save'
        car_add_headers = base_headers
        car_add_headers['Content-Type'] = 'application/json'
        car_add_data = {"tenantName": "铜川声威建材有限责任公司", "tenantCode": "", "licensePlate": license_plate, "axleType": "",
                        "axleVehicleType": "", "countryStandard": "", "deadweight": "", "emptyTare": "",
                        "annualSurveyMonthDate": "", "haveEtc": "", "haveSpeedWing": "", "isBlacklist": False,
                        "carCategory": "", "energyType": "", "tenantId": "2185", "tiepieceSite": "", "tiepieceCount": 1}
        result = requests.post(car_add_url, json=car_add_data, headers=car_add_headers)
        print("请求头：{}".format(car_add_headers))
        print("请求体：{}".format(car_add_data))
        print(str(result.content, 'utf-8'))
        if json.loads(result.content).get("message") == '车牌号已存在，请输入新的车牌！':
            list1.append(license_plate)
    print(list1)


if __name__ == '__main__':
    car_add()
